package com.yinghuo.risk.frigate.starter.config;

import com.yinghuo.risk.frigate.core.aspect.PermissionAspect;
import com.yinghuo.risk.frigate.core.aspect.RoleAspect;
import com.yinghuo.risk.frigate.core.handler.CacheHandler;
import com.yinghuo.risk.frigate.core.handler.TokenIdHandler;
import com.yinghuo.risk.frigate.core.info.UserLoginEvent;
import com.yinghuo.risk.frigate.core.info.UserLoginInfo;
import com.yinghuo.risk.frigate.core.test.TestConfig;
import com.yinghuo.risk.frigate.starter.properties.FrigateProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * com.yinghuo.risk.frigate.starter
 *
 * @author 白雨浓
 * date: 2018/10/17 20:35
 * content:
 */
@Configuration
@EnableConfigurationProperties(FrigateProperties.class)
public class FrigateAutoConfiguration {

    @Autowired
    private FrigateProperties frigateProperties;

    @Bean
    @ConditionalOnMissingBean
    public TestConfig frigateTest() {
        return new TestConfig(frigateProperties.getTest());
    }

    @Bean
    @ConditionalOnMissingBean
    public PermissionAspect permissionAspect(CacheHandler<? extends UserLoginInfo> cacheHandler, TokenIdHandler tokenIdHandler) {
        return new PermissionAspect(cacheHandler, tokenIdHandler);
    }

    @Bean
    @ConditionalOnMissingBean
    public RoleAspect roleAspect(CacheHandler<? extends UserLoginInfo> cacheHandler, TokenIdHandler tokenIdHandler) {
        return new RoleAspect(cacheHandler, tokenIdHandler);
    }

    @Bean
    @ConditionalOnMissingBean
    public UserLoginEvent userLoginEvent(CacheHandler<? extends UserLoginInfo> cacheHandler) {
        return new UserLoginEvent<>(cacheHandler);
    }

}
